﻿using System;
using System.Configuration;
using System.ServiceModel;
using TFile.ISA;

namespace TFile.CCA
{
    public class ClienteArquivo
    {
        private ChannelFactory<IDocumentLibraryService> _scf1;
        private ChannelFactory<IXMLDataService> _scf2;

        public ChannelFactory<IDocumentLibraryService> Scf1
        {
            get
            {
                _scf1.Credentials.Windows.ClientCredential.UserName = ConfigurationManager.AppSettings["usuariopasta"];
                _scf1.Credentials.Windows.ClientCredential.Password = ConfigurationManager.AppSettings["senhapasta"];
                return _scf1;
            }
            set { _scf1 = value; }
        }

        public ChannelFactory<IXMLDataService> Scf2
        {
            get { return _scf2; }
            set { _scf2 = value; }
        }

        public IDocumentLibraryService OpenChannel1(String rede)
        {
            NetTcpBinding ntcp = new NetTcpBinding();

            ntcp.MaxBufferPoolSize = 2147483647;
            ntcp.MaxReceivedMessageSize = 2147483647;
            ntcp.MaxBufferSize = 2147483647;
            ntcp.ReaderQuotas.MaxStringContentLength = 2147483647;
            ntcp.ReaderQuotas.MaxDepth = 2147483647;
            ntcp.ReaderQuotas.MaxBytesPerRead = 2147483647;
            ntcp.ReaderQuotas.MaxNameTableCharCount = 2147483647;
            ntcp.ReaderQuotas.MaxArrayLength = 2147483647;
            ntcp.SendTimeout = new TimeSpan(1, 10, 0);
            ntcp.ReceiveTimeout = new TimeSpan(1, 10, 0);

            Scf1 = new ChannelFactory<IDocumentLibraryService>(ntcp, rede);

            IDocumentLibraryService client;
            client = Scf1.CreateChannel();

            return client;
        }

        public IXMLDataService OpenChannel2(String rede)
        {
            NetTcpBinding ntcp = new NetTcpBinding();

            ntcp.MaxBufferPoolSize = 2147483647;
            ntcp.MaxReceivedMessageSize = 2147483647;
            ntcp.MaxBufferSize = 2147483647;
            ntcp.ReaderQuotas.MaxStringContentLength = 2147483647;
            ntcp.ReaderQuotas.MaxDepth = 2147483647;
            ntcp.ReaderQuotas.MaxBytesPerRead = 2147483647;
            ntcp.ReaderQuotas.MaxNameTableCharCount = 2147483647;
            ntcp.ReaderQuotas.MaxArrayLength = 2147483647;
            ntcp.SendTimeout = new TimeSpan(1, 10, 0);
            ntcp.ReceiveTimeout = new TimeSpan(1, 10, 0);

            Scf2 = new ChannelFactory<IXMLDataService>(ntcp, rede);

            IXMLDataService client;
            client = Scf2.CreateChannel();

            return client;
        }

        public void CloseChannel()
        {
            try
            {
                if (Scf1.State != CommunicationState.Faulted) Scf1.Close();
            }
            catch
            {
                Scf1.Abort();
            }

            try
            {
                if (Scf2.State != CommunicationState.Faulted) Scf2.Close();
            }
            catch
            {
                Scf2.Abort();
            }
        }
    }
}
